package beer.coder.walawala.gui.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import sun.awt.shell.ShellFolder;

/**
 * 图像相关辅助工具.
 *
 * @author amath0312
 * @version 1.0
 * @date 2019-10-25 00:28
 */
public class ImageUtil {

  public static Image extractWindowsFileImage(File file, int prefWidth, int prefHeight) throws IOException {
    ShellFolder sf = ShellFolder.getShellFolder(file);
    java.awt.Image icon = sf.getIcon(true);
    BufferedImage buffered = new BufferedImage(prefWidth, prefHeight, BufferedImage.TYPE_INT_ARGB);
    buffered.getGraphics().drawImage(icon, 0, 0, prefWidth, prefHeight, null);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(buffered, "png", baos);

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Image img = new Image(bais);
    return img;
  }
}
